package com.kevin.algorithm;


import com.kevin.model.Location;
import com.kevin.utils.DistanceUtil;
import com.kevin.utils.PointsRep;

import java.util.List;

/**
 * @Description: 多点连线不相交算法矫正算法
 *
 * @author kevin
 * @version V1.0
 * @date 2019/3/14 20:39
 **/
public class MultipointCorrectRoute implements AlgorithmStrategy{
    private static List<Location> list = null;

    @Override
    public Location[][] execute(PointsRep pointsRep) {
        list = pointsRep.getPoints();

        handleRoute(list);

        Location[][] locations = new Location[1][list.size()];
        for(int i=0;i<list.size();i++){
            locations[0][i] = list.get(i);
        }

        return locations;
    }

    /**
     * 矫正算法
     * @param locations
     */
    private void handleRoute(List<Location> locations){
        for(int i=0;i<list.size()-1;i++){
            //置换下标
            int index = -1;

            //获取当前点和下一点的距离
            double currDis = DistanceUtil.algorithm(locations.get(i),locations.get(i+1));

            for(int j=i+2;j<list.size();j++){
                double tempDis = DistanceUtil.algorithm(locations.get(i),locations.get(j));

                if(tempDis<currDis){
                    index = j;
                }
            }

            /**
             * 位置交换
             */
            if(index != -1){
                Location temp = list.get(index);
                list.add(i+1,temp);
                list.remove(index+1);
            }
        }
    }
}
